﻿@model IOA.MES.DataAccess.VModel.VCarryOverOrder

@{
    Layout = "~/Views/Shared/_LayoutPage.cshtml";
}

@section Contents{
    <style>
        .layui-footer {
            padding: 17px;
        }
    </style>
}

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label" style="width:150px;">车间： @(Model == null ? "" : Model.WorkshopName)</label>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label" style="width:230px;">转出工单： @(Model == null ? "" : Model.FromWorkOrderNo)</label>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label" style="width:230px;">转入工单： @(Model == null ? "" : Model.ToWorkOrderNo)</label>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label" style="width:230px;">操作人： @(Model == null ? "" : Model.CNCreatedBy)</label>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label" style="width:230px;">操作时间： @(Model == null ? "" : Model.CNCreatedTime)</label>
                </div>
            </div>
        </div>

        <div class="layui-card-body">
            <table id="tbCarryOverProducts" class="layui-table"></table>
        </div>

        <div class="layui-footer" @(ViewBag.IsReadonly == 1 ? "style=display:none;" : "")>
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="save">保存</button>
        </div>
    </div>
</div>

<input id="hidPkId" type="hidden" value="@(Model == null ? 0 : Model.PkId)" />
<input id="hidCarryOverNo" type="hidden" value="@(Model == null ? "" : Model.CarryOverNo)" />

@section Scripts{
    <script>
        layui.use(['table', 'form'], function () {
            var table = layui.table;
            var form = layui.form;

            var options = {
                elem: '#tbCarryOverProducts'
                , url: '/WorkOrder/QueryCarryOverProducts'
                , method: "post"
                , limit: 15
                , limits: [15, 20, 25, 30, 40, 50]
                , response: {
                    statusName: 'ErrCode'
                    , statusCode: 200
                    , msgName: 'ErrCode'
                    , countName: 'TotalCount'
                    , dataName: 'Data'
                }
                , where: {
                    carryOverNo: "@Model.CarryOverNo"
                }
                , cols: [[
                    { type: 'numbers', title: '序号', width: 60, align: 'center' }
                    , { field: 'ProductCode', title: '物料编码', align: 'center' }
                    , { field: 'ProductName', title: '物料名称', align: 'center' }
                    , { field: 'BatchNo', title: '批次号', align: 'center' }
                    , { field: 'Quantity', title: '数量', align: 'center', @Html.Raw(Model.Status == IOA.MES.DataAccess.Enum.CarryOverOrderStatus.UnConfirmed ? "edit: 'text'" : "")  }
                ]]
            };
            var tableIns = table.render(options);

            form.on('submit(save)', function (d) {
                var data = [];
                var carryOverNo = $('#hidCarryOverNo').val();
                var products = layui.table.cache.tbCarryOverProducts;
                for (var i = 0; i < products.length; i++) {
                    var product = products[i];
                    if (product.Quantity.toString().trim() != '' && isNaN(product.Quantity)) {
                        return layer.msg('数量必须为数字', { icon: 5 });
                    }

                    product.Quantity = product.Quantity.toString().trim() == '' ? 0 : parseFloat(product.Quantity);
                    if (product.Quantity > 0) {
                        data.push({
                            CarryOverNo: carryOverNo,
                            ProductCode: product.ProductCode,
                            ProductName: product.ProductName,
                            BatchNo: product.BatchNo,
                            Quantity: product.Quantity
                        });
                    }
                }

                var index = layer.load();
                $.ajax({
                    url: '/WorkOrder/SaveCarryOverProducts',
                    data: {
                        carryOverNo: '@Model.CarryOverNo',
                        products: data
                    },
                    dataType: 'json',
                    success: function (res) {
                        layer.close(index);
                        if (res.ErrCode != 200) {
                            return layer.msg(res.ErrMsg, { icon: 5 });
                        }

                        layer.msg('保存成功', { icon: 6 });
                    }
                });

            });
        });
    </script>
}
